<template>
	<view>
		<!-- 确认订单 -->
		<view class="bg-white px-2 pb-4">
			<!-- 步骤条 -->
			<view class="w-100 mt mb-2 flex justify-center">
				<uni-steps class="mt-2 mb-2" :active-color="color" :options="list1" :active="active" />
			</view>
			<!-- 订单明细 -->
			<view class="px-2 mb-3">
				<Navtitle style="background-color: rgb(253,246,236);" title="订单明细"></Navtitle>
				<view class="mt-2 flex flex-column" style="border: 2rpx solid rgb(221,221,211);">
					<view class="w-100 flex font-sm font-weight-bold"
						style="background-color: rgb(249,250,252);height: 70rpx;">
						<view class="flex-1 text-center"
							style="line-height: 70rpx;border-right:2rpx solid rgb(221,221,211);">
							<text>货物信息</text>
						</view>
						<view class=" text-center"
							style="width: 15%;line-height: 70rpx;border-right:2rpx solid rgb(221,221,211);">
							<text>单价</text>
						</view>
						<view class=" text-center"
							style="width: 15%;line-height: 70rpx;border-right:2rpx solid rgb(221,221,211);">
							<text>下单数量</text>
						</view>
						<view class=" text-center" style="width: 15%;line-height: 70rpx;">
							<text>金额</text>
						</view>
					</view>
					<view class="flex" style="border-bottom:2rpx solid rgb(221,221,211);height: 150rpx;"
						v-for="(item,index) in SalesOrder" :key="index">
						<view class="flex-1 flex ml-8 align-center text-ellipsis"
							style="line-height: 70rpx;border-right:2rpx solid rgb(221,221,211);">
							<image style="width: 100rpx;" :src="Domain_IMG+item.Picture_URL" mode="widthFix"></image>
							<text>{{item.Article_Name}}</text>
						</view>
						<view class="flex align-center justify-center"
							style="width: 15%;line-height: 70rpx;border-right:2rpx solid rgb(221,221,211);">
							<view class="flex justify-center">
								<text :class="'text-'+color">￥{{item.Sales_Unit_Price}}</text>
							</view>
						</view>
						<view class="flex align-center justify-center"
							style="width: 15%;line-height: 70rpx;border-right:2rpx solid rgb(221,221,211);">
							<text :class="'text-'+color">{{item.quantity}}</text>
						</view>
						<view class="flex align-center justify-center" style="width: 15%;line-height: 70rpx;">
							<text :class="'text-'+color">￥{{item.quantity * item.Sales_Unit_Price}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="px-2">
				<Navtitle style="background-color: rgb(203,246,236);" title="支付方式"></Navtitle>
				<radio-group @change="radioChange">
					<view class="flex p-4">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in payway" :key="item.name">
							<view class="flex align-center mr-2">
								<image style="width: 60rpx;" :src="item.imageUrl" mode="widthFix"></image>
								<view>{{item.name}}</view>
								<view>
									<radio :color="color" :value="item.name" :checked="index === current" />
								</view>
							</view>
						</label>
					</view>
				</radio-group>
			</view>
		</view>
		<view class="bg-white fixed-bottom flex justify-center align-center" style="height: 200rpx;">
			<view class="center flex align-center justify-center">
				<view  @click="back()" class="btn mt-2 mr-4 bg-white py-2 px-3 text-center font-sm"
				 :class="'border-'+color+' '+'text-'+color">
					<text>返回上一页</text>
				</view>
				<view @click="payfor()" class="btn flex flex-column align-center">
					<view class="mt-2 text-white py-2 px-3 text-center font-sm" :class="'bg-'+color+' '+'border-'+color" style="width: 100rpx;">
						<text>支付</text>
					</view>
				</view>
			</view>
		</view>
	    <PayMask @show="shows" :pay="pay" :payURL="payURL"></PayMask>
	</view>
</template>

<script>
	const app = getApp()
	import PayMask from '@/components/PayMask.vue'
	import times from '@/mixins/time.js'
	export default {
		data() {
			return {
				color: uni.getStorageSync('ThemeColors') ? uni.getStorageSync('ThemeColors') : 'green',
				Domain_IMG: app.globalData.Domain_IMG,
				active: 1,
				show: false,
				name:'[请登录]',
				checked: false,
				loading: false,
				pay: false,
				type:'error',
				SalesOrder: [],
				LetterCommitment: false,
				errmessage: '库存不足!',
				payURL: "",
				// 基础表单数据
				list1: [{
					title: '①确认订单'
				}, {
					title: '②在线付款'
				}],
				current: 0,
				payway: [{
						name: '微信支付',
						imageUrl: '/static/icon/WXlogo.png',
						check: true,
					},
					// {
					// 	name: '支付宝支付',
					// 	imageUrl: '/static/zfb.png',
					// 	check: false,
					// },
				],
				sales_order: {
					Sales_Order_SerialNumber: '',
					Store_SerialNumber: '',
					Sales_Date: '',
					Open_ID: '',
					Sales_Amount: '',
					Sales_Contacts: '',
					Sales_Phone: '',
					Sales_Address: '',
					Sales_Message: '',
					Collection_Amount: 0,
					Sales_ID: 'xiaoshou1',
					Receive_Type: 0,
					Refund_Type: 0,
					App_ID: '',
					Enterprise_Info_SerialNumber: app.globalData.enterprise,
					Children: {
						sales_order_detail: [{
							Sales_Order_Detail_SerialNumber: '',
							Sales_Order_SerialNumber: '',
							Sales_Unit_Price_Stock_SerialNumber: '',
							Article_SerialNumber: '',
							Sign: '无货货主',
							Quantity: '',
							Unit_Price: '',
							Sales_Unit_Price: '',
							Amount: '',
							Receive_Type: 0,
							Refund_Type: 0,
							Enterprise_Info_SerialNumber: app.globalData.enterprise,
							Article_Specifications_SerialNumber: '',
							Article_URL:'',
						}]
					}
				},
			};
		},
		onLoad(e) {
			this.SalesOrder = JSON.parse(decodeURIComponent(e.SalesOrder))
			console.log(this.SalesOrder)
		},
		components:{
			PayMask
		},
		methods:{
			changed(name) {
				console.log(name)
				if (name == '个人') {
					this.show = true
				} else {
					this.show = false
				}
			},
			payfor() {
				uni.showLoading({
					title:'加载中...'
				})
				let that = this
				if (!this.loading) {
					this.loading = true
					uni.request({ //检查库存
						header: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						url: app.globalData.Domain_Name + '/WX2CheckStockAction',
						method: "POST",
						data: {
							Data: JSON.stringify([{
								Sales_Unit_Price_Stock_SerialNumber: this.SalesOrder[0]
									.Sales_Unit_Price_Stock_SerialNumber,
								Quantity: this.SalesOrder[0].quantity,
							}])
						},
						success: (res) => {
							uni.hideLoading()
							console.log(res)
							if (res.data.Status == 'success') {
								this.addorder()
							} else if (res.data.Status == 'insufficient') {
								this.loading = false
								this.errmessage = '库存不足'
								this.LetterCommitment = true
								setTimeout(() => {
									this.LetterCommitment = false
								}, 3000)
							} else {
								this.loading = false
								this.errmessage = '提交失败'
								this.LetterCommitment = true
								setTimeout(() => {
									this.LetterCommitment = false
								}, 3000)
							}
						},
					})
				}
			},
			shows(e) {
				console.log(e)
				this.pay = e
				this.type = 'info'
				this.errmessage = '已取消支付'
				this.LetterCommitment = true
				setTimeout(()=>{
					this.LetterCommitment = false
				},3000)
			},
			addorder() {
				var token = uni.getStorageSync('token')
				this.sales_order.Sales_Date = times.nowtime()
				this.sales_order.Open_ID = uni.getStorageSync('use')
				this.sales_order.App_ID = app.globalData.appid
				this.sales_order.Enterprise_Info_SerialNumber = uni.getStorageSync('enterprise')
				this.sales_order.Store_SerialNumber = this.SalesOrder[0].Store_SerialNumber
				// this.sales_order.Sales_Address = this.SalesOrder[0].Sales_Address
				this.sales_order.Sales_Contacts = this.SalesOrder[0].Sales_Contacts
				this.sales_order.Sales_Phone = this.SalesOrder[0].Sales_Phone
				this.sales_order.Sales_Message = this.SalesOrder[0].Sales_Message
				this.sales_order.Sales_Amount = this.accMul( this.SalesOrder[0].quantity,this.SalesOrder[0].Sales_Unit_Price)
				this.sales_order.Children.sales_order_detail[0].Sales_Unit_Price_Stock_SerialNumber = this.SalesOrder[0].Sales_Unit_Price_Stock_SerialNumber
				this.sales_order.Children.sales_order_detail[0].Article_SerialNumber = this.SalesOrder[0].Article_SerialNumber
				this.sales_order.Children.sales_order_detail[0].Quantity = this.SalesOrder[0].quantity
				this.sales_order.Children.sales_order_detail[0].Unit_Price = this.SalesOrder[0].Unit_Price
				this.sales_order.Children.sales_order_detail[0].Sales_Unit_Price = this.SalesOrder[0].Sales_Unit_Price
				this.sales_order.Children.sales_order_detail[0].Article_Specifications_SerialNumber= this.SalesOrder[0].Article_Specifications_SerialNumber
				this.sales_order.Children.sales_order_detail[0].Amount = this.accMul( this.SalesOrder[0].quantity,this.SalesOrder[0].Sales_Unit_Price)
				this.sales_order.Children.sales_order_detail[0].Enterprise_Info_SerialNumber = uni.getStorageSync('enterprise')
				this.sales_order.Children.sales_order_detail[0].Article_URL = uni.getStorageSync('Article_URL')
				console.log(this.sales_order)
				uni.request({ //生成消耗订单
					url: app.globalData.Domain_Name + '/getAddAction',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: "POST",
					data: {
						Data: JSON.stringify({
							sales_order: [this.sales_order]
						}),
						token,
					},
					success: (res) => {
						console.log(res);
						if(res.data.Status=='success'){
							this.Pay(res.data.Data,token)
						}
					}
				})
			},
			Pay(Sales_Order_SerialNumber,token){
				uni.request({ //生成消耗订单
					url: app.globalData.Domain_Name + '/GroupWeChatNativeAction',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: "POST",
					data: {
						Data:Sales_Order_SerialNumber,
						Collection_Type:'微信支付',
						Order_Type:"1",
						token,
					},
					success: (res) => {
						console.log(res);
						this.payURL = res.data.code_url
						this.pay = true
					}
				})
			},
			radioChange: function(evt) {
				for (let i = 0; i < this.payway.length; i++) {
					if (this.payway[i].name === evt.detail.name) {
						this.current = i;
						break;
					}
				}
			},
			accMul(arg1, arg2) { //浮点型相乘
				let m = 0,s1 = arg1.toString(),s2 = arg2.toString();
				try {m += s1.split(".")[1].length} catch (e) {}
				try {m += s2.split(".")[1].length} catch (e) {}
				return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
			},
			back() {
				uni.navigateBack()
			}
		}

	}
</script>

<style lang="scss">

</style>
